package{
	import com.game.data.Data;
	import com.game.data.Stats;
	import com.game.events.GameEvent;
	import com.game.gui.HomePage;
	import com.game.gui.InterfaceControl;
	import com.game.main.MainGraphics;
	import com.game.sound.SoundManager;
	
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.system.SecurityDomain;
	import flash.utils.setTimeout;

	[SWF(width="800", height="500", frameRate="24", backgroundColor="#000000")]
	public class HS extends Sprite{
		[Embed(source='./assets/graphics.swf', mimeType="application/octet-stream")]
		public var SWF:Class;
		private var userInterface:InterfaceControl;
		private var mainGraphics:MainGraphics;
		private var soundManager:SoundManager;
		private var homeScreen:HomePage;
		public function HS(){
			var l:Loader = new Loader();
			var lc:LoaderContext = new LoaderContext();//false, ApplicationDomain.currentDomain
			lc.applicationDomain=ApplicationDomain.currentDomain;
			//LoaderContext.securityDomain = SecurityDomain.currentDomain, 
			lc.allowCodeImport=true;
			l.contentLoaderInfo.addEventListener(Event.COMPLETE, contentsRetrieve);
			l.loadBytes(new SWF(), lc);
			
		}
		/**
		 * Parses flash embeded elements
		 *
		 * @param e
		 *
		 */
		private function contentsRetrieve(e:Event):void {

			Data.getInstance().assets = MovieClip(e.target.content as MovieClip);
			if (this.stage) {
				this.stage.align = StageAlign.TOP_LEFT;
				//this.stage.scaleMode = StageScaleMode.NO_SCALE;
				this.stage.scaleMode = StageScaleMode.SHOW_ALL;
			}
			Data.getInstance().stageHeight = 500;
			Data.getInstance().stageWidth = 800;
			mainGraphics = new MainGraphics();
			addChild(mainGraphics);
			userInterface = new InterfaceControl();
			userInterface.addEventListener(GameEvent.EXIT_GAME,onExit);
			userInterface.addEventListener(GameEvent.GAME_OVER,onGameOver);
			soundManager = new SoundManager();
			addChild(userInterface);
			homeScreen = new HomePage();
			homeScreen.addEventListener(GameEvent.START_GAME, onStart);
			homeScreen.addEventListener(GameEvent.GAME_OVER, onExit);
			addChild(homeScreen);
			userInterface.onAdded();
			//addChild(new Stats());
		}
		private function onGameOver(e:Event):void {
			mainGraphics.stop();
			homeScreen.onEnd();
			//Data.getInstance().isPaused=true;
		}
		private function onExit(e:Event):void {
			homeScreen.onInit();
			mainGraphics.stopGame();
		}
		/**
		 * Initiates game
		 * @param e
		 *
		 */
		private function onStart(e:Event):void {
			mainGraphics.initiateGame();
			
		}
	}
}
